## The type of library we want to build. Possible values:
##   relocatable
##   static
LIBRARY_TYPE=static

# You may edit this makefile as long as you keep these original 
# target names defined.
MODE=debug
GNATMAKE=@GNATMAKE@
GNATCLEAN=gnatclean
SVN=svn
INSTALL=@INSTALL@

GPRPATH=asf.gpr

distdir=ada-asf-@ASF_VERSION@

DIST_FILE=ada-asf-@ASF_VERSION@.tar.gz

LN_S=@LN_S@
MKDIR=mkdir
CP=cp
LN=ln -s

ASF_SERVER=@ASF_SERVER@

ifeq (${OS},Windows_NT)
LIBEXT=dll
LIBVER=dll
else
LIBEXT=so
LIBVER=so.$(version)
endif

srcdir = .
top_srcdir = @top_srcdir@
VPATH = @srcdir@
prefix = @prefix@
exec_prefix = @exec_prefix@
top_builddir = .

dynamodir=${prefix}/share/dynamo
includedir=${prefix}/share/ada/adainclude
projectdir=${prefix}/share/ada/adainclude
bindir=${prefix}/bin
libdir=${prefix}/lib
alidir=${libdir}/ada/adalib/

MAKE_ARGS=-XMODE=${MODE} -XLIBRARY_TYPE=${LIBRARY_TYPE} -XPROCESSORS=4

ifeq (${BUILDS_SHARED},yes)
all:     static shared
install: install_static install_shared
else
all:     static
install: install_static
endif

# Build executables for all mains defined by the project.
build: regtests/asf-testsuite.adb
	$(GNATMAKE) -m -p -P "$(GPRPATH)" $(MAKE_ARGS)
	$(GNATMAKE) -m -p -Ptests $(MAKE_ARGS)

static:
	$(MAKE) LIBRARY_TYPE=static build

shared:
	$(MAKE) LIBRARY_TYPE=relocatable build

# Build and run the unit tests
test:	build
	$(GNATMAKE) -p -Ptests
	bin/asf_harness -xml asf-aunit.xml -config test.properties

regtests/asf-testsuite.adb: regtests/asf-testsuite.gpb Makefile
	gnatprep -DASF_SERVER=$(ASF_SERVER) regtests/asf-testsuite.gpb $@

# Not intended for manual invocation.
# Invoked if automatic builds are enabled.
# Analyzes only on those sources that have changed.
# Does not build executables.
autobuild:
	$(GNATMAKE) -gnatc -c -k  -P "$(GPRPATH)"

# Clean the root project of all build products.
clean:	clean_test
	-$(GNATCLEAN) -q -P "$(GPRPATH)" -XLIBRARY_TYPE=static
	-$(GNATCLEAN) -q -P "$(GPRPATH)" -XLIBRARY_TYPE=relocatable
	-rm -f lib/asf/*.a lib/asf/*.$(DLLEXT)
	-rm -f lib/asfunit/*.a lib/asfunit/*.$(DLLEXT)

# Clean the files produced by the unit tests
clean_test:
	rm -rf regtests/result/*
	$(GNATCLEAN) -q -Ptests

# Clean root project and all imported projects too.
clean_tree:
	$(GNATCLEAN) -P "$(GPRPATH)" -r

# Check *all* sources for errors, even those not changed.
# Does not build executables.
analyze:
	$(GNATMAKE) -f  -gnatc -c -k  -P "$(GPRPATH)"

# Clean, then build executables for all mains defined by the project.
rebuild: clean build


# Files to package in source distribution
DISTFILES=README NEWS LICENSE.txt NOTICE.txt CHANGES config.guess configure config.sub \
configure.in Makefile.in install-sh distrib test.properties ChangeLog \
asf.gpr.in asfunit.gpr.in tests.gpr.in samples.gpr.in config.gpr.in \
src asfunit samples regtests web bundles samples.properties dynamo.xml \
bin obj lib

dist:
	-rm -rf $(distdir)
	mkdir $(distdir)
	-chmod 777 $(distdir)
	here=`cd $(top_builddir) && pwd`; \
	top_distdir=`cd $(distdir) && pwd`; \
	distdir=`cd $(distdir) && pwd`; \
	for file in $(DISTFILES); do \
	  d=$(srcdir); \
	  if test -d $$d/$$file; then \
	    cp -pr $$d/$$file $(distdir)/$$file; \
	  else \
	    test -f $(distdir)/$$file \
	    || ln $$d/$$file $(distdir)/$$file 2> /dev/null \
	    || cp -p $$d/$$file $(distdir)/$$file || :; \
	  fi; \
	done
	-rm -rf $(distdir)/bin/*
	-rm -rf $(distdir)/obj/*
	-rm -rf $(distdir)/obj/asf/*
	-rm -rf $(distdir)/obj/asfunit/*
	-rm -rf $(distdir)/lib/*
	-rm -rf $(distdir)/lib/asf/*
	-rm -rf $(distdir)/lib/asfunit/*
	-rm -rf $(distdir)/regtests/result/*
	tar --exclude=.svn --exclude="*~" --exclude="*.o" --exclude="*.ali" \
	    --exclude="*.log" --exclude="*.a" \
	    -cvzf $(DIST_FILE) $(distdir)

install_static:
	$(MAKE) LIBRARY_TYPE=static install_lib

install_shared:
	$(MAKE) LIBRARY_TYPE=relocatable install_lib

install_lib:
	$(MKDIR) -p ${includedir}/asf
	$(MKDIR) -p ${includedir}/asfunit
	$(MKDIR) -p ${projectdir}/
	${MKDIR} -p ${alidir}/asf/$(LIBRARY_TYPE)
	${MKDIR} -p ${alidir}/asfunit/$(LIBRARY_TYPE)
	${MKDIR} -p ${dynamodir}/asf/bundles
	${MKDIR} -p ${dynamodir}/asf/web/js
	${MKDIR} -p ${dynamodir}/asf/web/js/jquery-ui
	${CP} -p asfunit/*.ad[bs] ${includedir}/asfunit
	${CP} -p src/*.ad[bs] ${includedir}/asf
	${CP} -p src/aws/*.ad[bs] ${includedir}/asf
	sed -e "s,INSTALL,${prefix},g" -e "s,LIBRARY_TYPE,$(LIBRARY_TYPE),g" < distrib/asf.gpr > ${projectdir}/asf.gpr
	sed -e "s,INSTALL,${prefix},g" -e "s,LIBRARY_TYPE,$(LIBRARY_TYPE),g" < distrib/asfunit.gpr > ${projectdir}/asfunit.gpr
	${CP} -p lib/asf/$(LIBRARY_TYPE)/*.ali ${alidir}/asf/$(LIBRARY_TYPE)
	${CP} -p lib/asfunit/$(LIBRARY_TYPE)/*.ali ${alidir}/asfunit/$(LIBRARY_TYPE)
ifeq ($(LIBRARY_TYPE),static)
	${CP} -p lib/asf/$(LIBRARY_TYPE)/libasf.a ${libdir}/
	${CP} -p lib/asfunit/$(LIBRARY_TYPE)/libasfunit.a ${libdir}/
else
	${CP} -p lib/asf/$(LIBRARY_TYPE)/libasf.$(LIBEXT) ${libdir}/
	${CP} -p lib/asfunit/$(LIBRARY_TYPE)/libasfunit.$(LIBEXT) ${libdir}/
	cd ${libdir} && \
	rm -f libasf.$(LIBEXT) libasfunit.$(LIBEXT) && \
	${LN} libasf.$(LIBVER) libasf.$(LIBEXT) && \
	${LN} libasfunit.$(LIBVER) libasfunit.$(LIBEXT)
endif
	(cd web && tar --exclude=.svn --exclude='*~' -cf - . )| (cd ${dynamodir}/asf/web && tar xf -)
	${CP} bundles/*.properties ${dynamodir}/asf/bundles/
	${CP} dynamo.xml ${dynamodir}/asf/
	${CP} NOTICE.txt ${dynamodir}/asf/
	${CP} LICENSE.txt ${dynamodir}/asf/

uninstall:
	rm -rf ${includedir}/asf
	rm -rf ${includedir}/asfunit
	rm -rf ${alidir}/asf ${alidir}/asfunit
	rm -f ${libdir}/libasf.a
	rm -f ${libdir}/libasfunit.a
	rm -f ${libdir}/libasf.$(LIBEXT)
	rm -f ${libdir}/libasfunit.$(LIBEXT)
	rm -f ${projectdir}/asf.gpr
	rm -f ${projectdir}/asfunit.gpr
	rm -rf ${awadir}/web

# prepare before releasing a new version of the package
# (collect information about changes and SVN repository)
prepare-release:
	${SVN} update
	${SVN} info > CHANGES
	${SVN} log -v > ChangeLog
